<template>
    <el-main>
        <el-form :model="searchModel" ref="searchForm" label-width="80px" :inline="true" size="small">
            <el-form-item>
                <el-input v-model="searchModel.key" placeholder="请输入" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" plain icon="el-icon-search" @click="search(pageNo, pageSize)">查询
                </el-button>
                <el-button icon="el-icon-refresh-right" @click="resetValue">重置</el-button>
                <el-button v-if="isAdmin !== 1" type="success" plain icon="el-icon-plus"
                    @click="openAddWindow()">新增</el-button>
            </el-form-item>
        </el-form>
        <!-- 数据表格 -->
        <el-table :data="attentList" :height="tableHeight" border stripe style="width: 100%; margin-bottom: 10px">
            <el-table-column prop="id" label="序号" width="100" align="center"></el-table-column>
            <el-table-column prop="userName" label="创建人"></el-table-column>
            <el-table-column prop="reason" label="请假
            原因"></el-table-column>
            <el-table-column prop="createTime" label="创建时间"></el-table-column>
            <el-table-column prop="startTime" label="开始时间"></el-table-column>
            <el-table-column prop="endTime" label="结束时间"></el-table-column>
            <el-table-column prop="step" label="状态">
                <template slot-scope="scope">
                    <el-tag :type="scope.row.step == '1' ? 'success' : scope.row.step == '0' ? 'warning' : 'danger'
            " disable-transitions>{{
            scope.row.step == '1'
                ? '审核通过'
                : scope.row.step == '0'
                    ? '待审核'
                    : '审核不通过'
        }}</el-tag>
                </template>
            </el-table-column>


            <el-table-column label="操作" align="center" width="240">
                <template slot-scope="scope">
                    <el-button v-if="isAdmin !== 1" icon="el-icon-edit" type="primary" plain size="small"
                        @click="handleEdit(scope.row)" :disabled="scope.row.step !== 0">编辑
                    </el-button>
                    <el-button v-if="isAdmin !== 1" icon="el-icon-delete" type="danger" plain size="small"
                        @click="handleDelete(scope.row)" :disabled="scope.row.step !== 0">删除
                    </el-button>


                    <el-button v-if="isAdmin === 1" icon="el-icon-edit" type="success" plain size="small"
                        @click="handleApprove(scope.row)">审核通过
                    </el-button>
                    <el-button v-if="isAdmin === 1" icon="el-icon-delete" type="danger" plain size="small"
                        @click="handleNotApprove(scope.row)">审核不通过
                    </el-button>
                </template>
            </el-table-column>

        </el-table>
        <!-- 分页工具栏 -->
        <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
            :current-page="pageNo" :page-sizes="[10, 20, 30, 40, 50]" :page-size="10"
            layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
        <!-- 添加和修改请假窗口 -->
        <system-dialog :title="attentDialog.title" :visible="attentDialog.visible" :width="attentDialog.width"
            :height="attentDialog.height" @onClose="onClose" @onConfirm="onConfirm">
            <div slot="content">
                <el-form :model="attent" ref="attentForm" :rules="rules" label-width="80px" :inline="false"
                    size="small">
                    <el-form-item label="请假原因" prop="reason">
                        <el-input type="text" v-model="attent.reason"></el-input>
                    </el-form-item>
                    <el-form-item label="开始时间" prop="startTime">
                        <el-date-picker v-model="attent.startTime" type="date" placeholder="请选择日期" format="yyyy/MM/dd"
                            value-format="yyyy-MM-dd hh:mm:ss" />
                    </el-form-item>
                    <el-form-item label="结束时间" prop="endTime">
                        <el-date-picker v-model="attent.endTime" type="date" placeholder="请选择日期" format="yyyy/MM/dd"
                            value-format="yyyy-MM-dd hh:mm:ss" />
                    </el-form-item>
                </el-form>
            </div>
        </system-dialog>

    </el-main>
</template>

<script>
import attentApi from "@/api/attent";
import userApi from "@/api/user";
import { mapGetters } from 'vuex';
//导入对话框组件
import SystemDialog from "@/components/system/SystemDialog.vue";
export default {
    name: "attentList",
    components: {
        SystemDialog,
    },
    data() {
        return {
            //查询条件
            searchModel: {
                key: "",
                pageNo: 1,
                pageSize: 10,
                userIds: []
            },
            employeeOptions: [],
            rules: {
                // title: [
                //     { required: true, trigger: "blur", message: "请输入" },
                // ],
                reason: [
                    { required: true, trigger: "blur", message: "请输入" },
                ],
                startTime: [
                    { required: true, trigger: "blur", message: "请输入" },
                ],
                endTime: [
                    { required: true, trigger: "blur", message: "请输入" },
                ],

            },
            //添加和修改请假窗口属性
            attentDialog: {
                title: "",
                visible: false,
                height: 230,
                width: 500,
            },
            // 对象
            attent: {
                id: "",
                reason: "",
                startTime: "",
                endTime: "",
                userId: this.$store.getters.userId,
                //userName: "",
                // createUser: this.$store.getters.userId,
            },
            attentList: [], //数据列表
            deptions: [],
            tableHeight: 0, //表格高度
            pageNo: 1, //当前页码
            pageSize: 10, //每页显示数量
            total: 0, //总数量
        };
    },
    methods: {
        /**
         * 查询
         */
        async search(pageNo = 1, pageSize = 10) {
            this.searchModel.pageNo = pageNo; //当前页码
            this.searchModel.pageSize = pageSize; //每页显示数量
            //发送查询请求
            let res = await attentApi.getleaveListApi(this.searchModel);
            //执行成功
            if (res.success) {
                //请假列表
                this.attentList = res.data.records;
                //总数量
                this.total = res.data.total;
            }
        },
        /**
          * 获取员工名单
          */
        async getStaff() {
            //发送查询请求
            let res = await userApi.getUserAllList({});
            //执行成功
            if (res.success) {
                console.log(res)
                //员工列表
                this.employeeOptions = res.data;
            }
        },

        /**
         * 重置查询条件
         */
        resetValue() {
            //清空查询条件
            this.searchModel.key = null;
            //重新查询
            this.search();
        },
        /**
         * 当每页数量发生变化时触发该事件
         */
        handleSizeChange(size) {
            this.pageSize = size; //将每页显示的数量交给成员变量
            this.search(this.pageNo, size);
        },
        /**
         * 当页码发生变化时触发该事件
         */
        handleCurrentChange(page) {
            this.pageNo = page;
            //调用查询方法
            this.search(page, this.pageSize);
        },
        /**
         * 打开添加窗口
         */
        openAddWindow() {
            //清空表单数据
            this.$resetForm("attentForm", this.attent);
            this.attentDialog.title = "新增请假信息"; //设置窗口标题
            this.attentDialog.visible = true; //显示窗口
        },
        /**
         * 窗口取消事件
         */
        onClose() {
            this.attentDialog.visible = false;
        },
        handleEdit(row) {
            //数据回显
            this.$objCopy(row, this.attent); //将当前编辑的数据复制到attent对象中
            //设置窗口标题
            this.attentDialog.title = "编辑请假信息";
            //显示编辑请假窗口
            this.attentDialog.visible = true;
        },
        //审核通过
        async handleApprove(row) {
            let res = await attentApi.getApproveListApi({ leaveAnagementApproves: [{ leaveAnagementId: row.id, step: 1 }] });
            //判断是否成功
            if (res.success) {
                this.$message.success(res.message);
                //关闭窗口
            } else {
                this.$message.error(res.message);
            }
            this.search();
        },
        async handleNotApprove(row) {
            let res = await attentApi.getApproveListApi({ leaveAnagementApproves: [{ leaveAnagementId: row.id, step: 2 }] });
            //判断是否成功
            if (res.success) {
                this.$message.success(res.message);
                //关闭窗口
            } else {
                this.$message.error(res.message);
            }
            this.search();
        },
        /**
            * 新增或编辑确认事件
            */
        onConfirm() {
            this.$refs.attentForm.validate(async (valid) => {
                if (valid) {
                    let res = null;
                    //判断用户ID是否为空
                    this.attent.userId = this.$store.getters.userId
                    res = await attentApi.getSaveListApi(this.attent);
                    //判断是否成功
                    if (res.success) {
                        this.$message.success(res.message);
                        //关闭窗口
                    } else {
                        this.$message.error(res.message);
                    }
                    this.attentDialog.visible = false;
                    this.search();
                }
            });
        },
        /**
* 删除
 
*/

        async handleDelete(row) {
            //查询部门下是否存在子部门或用户
            let res = await attentApi.getDeleteListApi({ mainIds: [row.id] });
            //判断是否可以删除
            if (res.success) {
                //成功提示
                this.$message.success(res.message);
                //刷新
                this.search();
            } else {
                //失败提示
                this.$message.error(res.message);
            }
        }


    },
    computed: {
        ...mapGetters([
            'name',
            'avatar',
            'roles',
            'isAdmin'
        ])
    },
    created() {
        //调用查询请假列表的方法
        this.search();

    },
    mounted() {
        this.$nextTick(() => {
            this.tableHeight = window.innerHeight - 220;
            this.getStaff();
        });
    },
};
</script>

<style></style>